/*
  Copyright (c) 2013 LEROY Anthony <leroy.anthony@gmail.com>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program. If not, see http://www.gnu.org/licenses/gpl.txt
*/

package fr.sigma.labo.autostoptan;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends Activity {

    private Location location;

    private void afficherLocation(Location location) {
        if (location != null) {
            ((TextView)findViewById(R.id.lat)).setText("Lat: "+location.getLatitude());
            ((TextView)findViewById(R.id.lon)).setText("Lon: "+location.getLongitude());
        }
    }

    private void envoyerDonnees(Location location) {
        if (location != null) {
            JSONObject data = new JSONObject();
            try {
                data.put("lon", location.getLongitude());
                data.put("lat", location.getLatitude());

                JSONObject json = new JSONObject();
                json.put("id", "test");
                json.put("data", data);
                new SendDataTask().execute(json);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        new GetDataTask((TextView)findViewById(R.id.json)).execute();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setPowerRequirement(Criteria.POWER_LOW);

        String provider = locationManager.getBestProvider(criteria, true);
        if (provider != null) {
            System.out.println("provider:"+provider);
            LocationListener locationListener = new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    afficherLocation(location);
                    envoyerDonnees(location);
                }

                @Override
                public void onProviderDisabled(String arg0) {
                    // TODO Auto-generated method stub
                    System.out.println("onProviderDisabled");
                }

                @Override
                public void onProviderEnabled(String arg0) {
                    // TODO Auto-generated method stub
                    System.out.println("onProviderEnabled");
                }

                @Override
                public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                    // TODO Auto-generated method stub
                    System.out.println("onStatusChanged");
                }
            };

            locationManager.requestLocationUpdates(provider, 0, 0, locationListener);
            location = locationManager.getLastKnownLocation(provider);
            afficherLocation(location);
            envoyerDonnees(location);

            final Button button = (Button) findViewById(R.id.send);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    envoyerDonnees(location);
                }
            });

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
